/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.yami.shop.bean.app.param.PayParam;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.pay.PayInfoDto;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author lanhai
 */
@RestController
@RequestMapping("/p/order")
@Tag(name = "订单接口")
@AllArgsConstructor
public class PayController {


    private final PayService payService;
    //需配置好相关配置后打开 com.yami.shop.common.config.WxPayConfiguration文件
    //private final WxPayService wxService;

    /**
     * 支付接口
     */
    @PostMapping("/pay")
    @Operation(summary = "根据订单号进行支付" , description = "根据订单号进行支付")
    public ServerResponseEntity<Void> pay(@RequestBody PayParam payParam) {
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();


        PayInfoDto payInfo = payService.pay(userId, payParam);
        payService.paySuccess(payInfo.getPayNo(), "");
        return ServerResponseEntity.success();
    }

    /**
     * 普通支付接口
     */
    @PostMapping("/normalPay")
    @Operation(summary = "根据订单号进行支付" , description = "根据订单号进行支付")
    public ServerResponseEntity<Boolean> normalPay(@RequestBody PayParam payParam) {

        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();
        PayInfoDto pay = null;//payService.pay(userId, payParam);

        // 根据内部订单号更新order settlement
        payService.paySuccess(pay.getPayNo(), "");

        return ServerResponseEntity.success(true);
    }

    /**
     * 微信支付
     * @param payAmount 订单金额
     * @param orderNumber 订单编号
     * @param title 订单标题
     * @param notifyUrl 回调地址
     * @return 微信支付信息
     */
    public WxPayMpOrderResult wxPay(BigDecimal payAmount, String orderNumber, String title, String notifyUrl){
        BigDecimal payAmountTemp = payAmount.setScale(2, RoundingMode.DOWN);
        User userByUserId = null;//userService.getById(SecurityUtils.getUserId());
        int payMoney = 1;
        BigDecimal multiply = payAmountTemp.multiply(new BigDecimal("100"));

        WxPayMpOrderResult wxPayMpOrderResult;

        try {
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder()
                    .outTradeNo(orderNumber)
                    .body(title)
                    .notifyUrl(notifyUrl)
                    .totalFee(multiply.intValue())
                    .spbillCreateIp("202.104.160.233")
                    .openid("userByUserId.getOpenId()")
                    .tradeType("JSAPI")
                    .build();
            wxPayMpOrderResult = null;//wxService.createOrder(wxPayUnifiedOrderRequest);
        } catch (Exception e) {
            throw new YamiShopBindException("微信支付错误", e);
        }

        return wxPayMpOrderResult;
    }
}
